/*
 * ClientUIController.java
 * Created on 28-Mar-2011, 12:16:18 PM
 */
package pos.desktop.controller;


import pos.entity.Category;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.tree.DefaultMutableTreeNode;
import pos.interfaces.Database;
import pos.storage.StorageFactory;
import pos.storage.db.sql.SQL;
import pos.entity.Item;

/**
 * @since 30 MARCH 2011, 5:00 PM
 * @author Anup Mathew
 * <b>Description:<b> <br>This Class is used as a controller for the applet
 * UI, which allows the customer to purchase and deliver items to a specified
 * address. This controller is invoked by the ClientUI.
 */
public class ClientUIController {

    //private global variables
    private HashMap categories = new HashMap();
    private HashMap items = new HashMap();
    private ArrayList itemsList = new ArrayList();

    public ArrayList getItemsList() {
        return itemsList;
    }

    public void setItemsList(ArrayList itemsList) {
        this.itemsList = itemsList;
    }

    /**
     * @since 30 MARCH 2011, 5:00 PM
     * @param root
     * @param parentId 
     */
    public void getCategories(DefaultMutableTreeNode root, int parentId) {

        //Getting a DB Connection
        try {
            Database mysql = (Database) StorageFactory.getStorage(StorageFactory.DBTYPE, Database.MYSQL);
            Connection custConn = mysql.getConnection(Database.CUSTOMER);

            PreparedStatement ps = (PreparedStatement) custConn.prepareStatement("SELECT * FROM category where CategoryParentId = ?");
            ps.setInt(1, parentId);

            ResultSet rs = ps.executeQuery();

            while (rs.next()) {
                Category category = new Category();
                category.setCategoryId(rs.getInt(1));
                category.setParentCategoryId(rs.getInt(3));
                category.setName(rs.getString(2));
                categories.put(category.getName(), category);
                DefaultMutableTreeNode child = new DefaultMutableTreeNode(category.getName());
                root.add(child);
                getCategories(child, rs.getInt(1));
            }

        } catch (SQLException ex) {
            Logger.getLogger(ClientUIController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ClientUIController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * @since 30 MARCH 2011, 5:00 PM
     * @return 
     */
    public HashMap getCategories() {
        return categories;
    }

    /**
     * @since 30 MARCH 2011, 5:00 PM
     * @param categories 
     */
    public void setCategories(HashMap categories) {
        this.categories = categories;
    }

    /**
     * @since 30 MARCH 2011, 5:00 PM
     * @param categoryName
     * @return 
     */
    public ArrayList getItems(String categoryName) {
        Category category = (Category) getCategories().get(categoryName);
        items = new HashMap();
        ArrayList listOfItem = new ArrayList();
        try {
            Database mysql = (Database) StorageFactory.getStorage(StorageFactory.DBTYPE, Database.MYSQL);
            Connection custConn = mysql.getConnection(Database.CUSTOMER);
            
            String sqlQuery = SQL.GET_ITEM_INFO;

            PreparedStatement ps = (PreparedStatement) custConn.prepareStatement
                    (sqlQuery);
            ps.setInt(1, category.getCategoryId());

            ResultSet rs = ps.executeQuery();

            while (rs.next()) {
                long itemId = rs.getLong(3);
                Item item = getItemDetails(itemId);
                items.put(item.getName(), item);
                listOfItem.add(item.getName());
            }

        } catch (SQLException ex) {
            Logger.getLogger(ClientUIController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ClientUIController.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return listOfItem;
    }

    /**
     * @since 30 MARCH 2011, 5:00 PM
     * @param itemId
     * @return 
     */
    private static Item getItemDetails(long itemId) {
        Item item = new Item();
        try {
            Database mysql = (Database) StorageFactory.getStorage(StorageFactory.DBTYPE, Database.MYSQL);
            Connection custConn = mysql.getConnection(Database.CUSTOMER);
            
            String sqlQuery = SQL.SELECT_ITEM_DETAILS;

            PreparedStatement ps = (PreparedStatement) custConn.prepareStatement
                    (sqlQuery);
            ps.setLong(1, itemId);

            ResultSet rs = ps.executeQuery();

            while (rs.next()) {
                item.setID(rs.getLong("ItemId"));
                item.setName(rs.getString("ItemName"));
                item.setDesc(rs.getString("ItemDescription"));
                item.setUPCode(rs.getString("UPC"));
                item.setBasePrice(rs.getDouble("Price"));
                item.setDiscount(rs.getDouble("Discount"));
            }

        } catch (SQLException ex) {
            Logger.getLogger(ClientUIController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ClientUIController.class.getName()).log(Level.SEVERE, null, ex);
        }

        return item;
    }

    /**
     * @since 30 MARCH 2011, 5:00 PM
     * @param itemName
     * @return 
     */
    public  Item updateItemDetails(String itemName) {
        Item item  = (Item) getItems().get(itemName);
        return item;
    }
    
    /**
     * @since 30 MARCH 2011, 5:00 PM
     * @return 
     */
    public HashMap getItems() {
        return items;
    }

    /**
     * @since 30 MARCH 2011, 5:00 PM
     * @param items 
     */
    public void setItems(HashMap items) {
        this.items = items;
    }

    public void addItemToCart(Item item, ArrayList itemList) {
        //Set the item selected into the order structure
        itemList.add(item);
    }
}